package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){

        dishService.saveWithFlavor(dishDto);

        return R.success("添加菜品成功");
    }

    /**
     * 菜品信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        //构造分页条件对象
        Page<Dish> dishPage = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        //执行分页条件查询
        dishService.page(dishPage,queryWrapper);

        //定义DishDto的分页对象
        Page<DishDto> dishDtoPage = new Page<>(page, pageSize);

        //创建一个集合，用来储存所有DishDto的对象们
        ArrayList<DishDto> dishDtos = new ArrayList<>();

        //将每一个Dish中的对象，转成dishDto对象
        List<Dish> dishes = dishPage.getRecords();
        for (Dish dish : dishes) {
            DishDto dishDto = new DishDto();

            //赋值每一个Dish的所有片段到DishDto中
            BeanUtils.copyProperties(dish,dishDto);

            //从dish对象中获取分类id
            Long categoryId = dish.getCategoryId();
            //从分类id获取分类对象
            Category categoryForName = categoryService.getById(categoryId);
            //从分类对象中获取分类名称，赋值给DishDto中的categoryName属性
            dishDto.setCategoryName(categoryForName.getName());

            //将赋值了成员变量的dishDto对象放入到dishDtos集合中
            dishDtos.add(dishDto);
        }

        //将dishDto的集合，放入到page<DishDto>中，作为分页数据
        dishDtoPage.setRecords(dishDtos);
        //将之前查询到的total数据，放到dto的page中
        dishDtoPage.setTotal(dishPage.getTotal());

        return R.success(dishDtoPage);
    }

    /**
     * 根据条件查询对应的菜品数据
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<Dish>> list(Dish dish){
        //构造查询条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件，查询状态为1（起售状态）的菜品
        queryWrapper.eq(dish.getCategoryId() != null,
                Dish::getCategoryId,dish.getCategoryId());
        //添加排序条件
        queryWrapper.eq(Dish::getStatus,1);
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> list = dishService.list(queryWrapper);

        return R.success(list);
    }
}
